import { fromJS } from 'immutable'; // 引入immutable.js库，防止state对象被错误赋值

// immutable对象
const defaultState = fromJS({
    focused: false,
    mouseIn: false,
    searchVal: '',
    list: [],
    page: 1,
    totalPage: 1,
});

const reducer = (state = defaultState, action) => {
    switch (action.type) {
        case 'SEARCH_FOCUS':
            return state.set('focused', action.focused);
        case 'INIT_LIST':
            // 设置单个
            // return state.set('list', action.list).set('totalPage', action.totalPage);
            // 设置多个
            return state.merge({
                list: action.list,
                totalPage: action.totalPage,
            });
        case 'MOUSE_ENTER':
            return state.set('mouseIn', action.mouseIn);
        case 'CHANGE_PAGE':
            return state.set('page', action.page);
        case 'SEARCH_VALUE':
            return state.set('searchVal', action.value);
        default:
            return state;
    }
};

export default reducer;
